﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for BusinessLogo
    /// </summary>
    public class BusinessLogo : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function
        #region private method


        //Select all data
        public void GetLogoList(string Company)
        {
            string strJson = null;
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = BusinessLogoHandler.GetLogoList(pageIndex, pageSize,Company); 
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Add new business logo
        public int AddBusinessLogo(string ImageFileName, string Company, string Description)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = BusinessLogoHandler.AddBusinessLogo(ImageFileName, Company, Description);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Edit business logo
        public int EditBusinessLogo(int ID, string ImageFileName, string Company, string Description)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = BusinessLogoHandler.EditBusinessLogo(ID, ImageFileName, Company, Description);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Delete business logo
        public bool DeleteBusinessLogo(int ID)
        {
            bool flag = false;
            try
            {
                flag = BusinessLogoHandler.DeleteBusinessLogo(ID);
            }
            catch (Exception ex) 
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        //Select business logo
        public void SelectBusniessLogo(string Company)
        {
            DataTable dt;
            try
            {
                dt = BusinessLogoHandler.SelectBusniessLogo(Company);
                string strJson = JsonConvert.SerializeObject(dt);

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }


        //Select theme according to company
        public void SelectTheme(string Company)
        {
            DataTable dt;
            try
            {
                dt = BusinessLogoHandler.SelectTheme(Company);
                string strJson = JsonConvert.SerializeObject(dt);

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
      


        #endregion
    }
}